package com.leetcode.interview;

import java.util.Arrays;

/**
 * @program: 消失的两个数字
 * @description: https://leetcode.cn/problems/missing-two-lcci/
 * @author: Wang Zhihua
 * @date: 2022-09-26
 */
public class 消失的两个数字 {

    public int[] missingTwo(int[] nums) {
        int n = nums.length + 2;

        int x1orx2 = 0;
        for (int num : nums) {
            x1orx2 ^= num;
        }
        for (int i = 1; i<= n; ++i) {
            x1orx2 ^= i;
        }

        int andNum = x1orx2 & (-x1orx2);
        int x1 = 0, x2 = 0;
        for (int num : nums) {
            if ((num & andNum) == 0) {
                x1 ^= num;
            } else {
                x2 ^= num;
            }
        }
        for (int i = 1; i <= n; ++i) {
            if ((i & andNum) == 0) {
                x1 ^= i;
            } else {
                x2 ^= i;
            }
        }

        return new int[]{x1, x2};
    }

    public static void main(String[] args) {
        消失的两个数字 solution = new 消失的两个数字();

        int[] nums = new int[]{2,3};

        System.out.println(Arrays.toString(solution.missingTwo(nums)));
    }

}
